import java.net.*;  
import java.nio.channels.FileChannel;
import java.io.*;
import java.util.*;

public class SimpleServer {

	public static void main(String args[]) {
		int port = 2002;
		try {
			ServerSocket ss = new ServerSocket(port);
			Socket s = ss.accept();
			InputStream is = s.getInputStream();
			ObjectInputStream ois = new ObjectInputStream(is);
			File to = (File) ois.readObject();
			if (to != null) {
				printFileInfo(to);
				File newFile = new File("copy/"+to.getName());
				FileImporter.copyFile(to,newFile);

			}
			//System.out.println("Server:" + (String) ois.readObject());
			is.close();
			s.close();
			ss.close();
		} catch (Exception e) {
			System.out.println(e);
		}
	}
	
	
	   public static void copyFile(File in, File out) throws IOException {
	       FileChannel inChannel = new FileInputStream(in).getChannel();
	       FileChannel outChannel = new FileOutputStream(out).getChannel();
	       try {
	           inChannel.transferTo(0, inChannel.size(), outChannel);
	       } catch (IOException e) {
	           throw e;
	       } finally {
	           if (inChannel != null)
	               inChannel.close();
	           if (outChannel != null)
	               outChannel.close();
	       }
	   }
	   
	   static void printFileInfo(File fil)
	   {
	      System.out.println("Name= "+fil.getName());
	      System.out.println("Path= "+fil.getPath());
	      System.out.println("AbsolutePath= "+fil.getAbsolutePath());
	      System.out.println("Parent= "+fil.getParent());
	      System.out.println("exists= "+fil.exists());
	      System.out.println("canWrite= "+fil.canWrite());
	      System.out.println("canRead= "+fil.canRead());
	      System.out.println("isFile= "+fil.isFile());
	      System.out.println("isAbsolute= "+fil.isAbsolute());
	      System.out.println("lastModified= "+(new Date(fil.lastModified()))  );
	      System.out.println("length= "+fil.length());
	      
	      
	      System.out.println("isDirectory= "+fil.isDirectory());
	      if (fil.isDirectory()) {
	         String fils[] = fil.list();
	         for (int i=0; i<fils.length; ++i) {
	            System.out.println("src/"+fils[i]);
	            
	            File fl = new File("src/"+fils[i]);
	            printFileInfo(fl);
	            
	         }
	      }
	      else
	      {
	    	  
	      }	      
	      
	      System.out.println("");
	   }
}



